#include "stdafx.h"
#include "Misc.h"
#include <cmath>
string errMsg = "No Error.";
string msg[] = 
{"Error: Number of Composite Vents is Larger than 1000",
"Error: The configurations of certain NCUs are not assigned.",
"Error: The number of input parameters does not comform to the required number.",
"Error: No such a NCU, please check the configurations.",
"Error: No such a character for the identity of a NCU, please check the configurations.",
""};
double threshold(double a, double b)
{
	return (a > b) ? (a-b): 0;
}

/*string strToken(string &inStr, const char delim)
{
	string a = ""; // empty string
	while (inStr[0] == delim) // process the beginning delim
		inStr.erase(0,1);

	while ((inStr[0] != delim) && (!inStr.empty()))
	{
		a += inStr[0];
		inStr.erase(0,1);
	}
	return a;
} // end strToken
*/
float coe(const vector<double>& evalRunoff, const vector<double>& obsRunoff)
{
	vector<double>::const_iterator cur1 = obsRunoff.begin();
	double mean = 0;
	while (cur1 != obsRunoff.end())
	{
		mean += (*cur1);
		++cur1;
	}
	mean /= obsRunoff.size();
	cur1 = obsRunoff.begin();
	vector<double>::const_iterator cur2 = evalRunoff.begin();
	double a, b;
	a = 0;
	b = 0;
	while (cur1 != obsRunoff.end())
	{
		a += pow(
			(
			(*cur1)-(*cur2)
			),2
			);
		b +=pow(
			(
			(*cur1)-(mean)
			),2
			);
		++cur1;
		++cur2;
	}
	return 1-(a/b);
}
float sls(const vector<double>& evalRunoff,
		  const vector<double>& obsRunoff)
{
	vector<double>::const_iterator cur1 = evalRunoff.begin();
	vector<double>::const_iterator cur2 = obsRunoff.begin();
	float a;
	a = 0;
	while (cur1 != evalRunoff.end())
	{
		a += pow(
			((*cur1)-(*cur2)),2
			);
		++cur1;
		++cur2;
	}
	return a;
}

void move(vector<double>::const_iterator &iter, const int &steps)
{
	for ( int i = 0; i < steps; ++ i)
	{
		++ iter;
	}
}

double accuracy(const double upper, const double lower, const unsigned int length)
{
	return ( ( upper - lower) /
        ( pow(2.0, double(length)) - 1)
		);
}
unsigned int 
digits(const double upper, const double lower, const double acu)
{
	double pre = acu +1 ;
	unsigned int i;
	for (i = 0; pre >= acu; ++ i)
	{
		pre = accuracy(upper, lower, i+1);
	}
	return i;
}
/*doubleChromosome partial(int number, const doubleChromosome* a, doubleChromosome::iterator& cur)
{
	doubleChromosome b;
	cur = a->begin();
	for (int i = 0; i < number; ++i)
	{
		b.push_back(*cur);
		++cur;*/